package com.bms.utils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bms.entity.App;
import com.bms.entity.AuditResult;
import com.bms.entity.FtpServer;
import com.bms.entity.IPRange;
import com.bms.entity.Role;
import com.bms.entity.User;
import com.bms.service.AppService;
import com.bms.service.AuditResultService;
import com.bms.service.FtpServerService;
import com.bms.service.IPRangeService;
import com.bms.service.RoleService;
import com.bms.service.UserService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/spring-context.xml" })
public class InitSystem {
	
	protected Logger logger = Logger.getLogger(InitSystem.class);
	
	@Autowired
	private RoleService roleService;

	@Autowired
	private UserService userService;

	@Autowired
	private AppService appService;

	@Autowired
	private FtpServerService ftpServerService;

	@Autowired
	private AuditResultService auditResultService;

	@Autowired
	private IPRangeService ipRangeService;

	private void clearData() {
		// 删除用户
		List<User> userList = userService.getAll();
		for (User entity : userList) {
			userService.delete(entity);
		}
		// 删除应用审核结果
		List<AuditResult> auditList = auditResultService.getAll();
		for (AuditResult entity : auditList) {
			auditResultService.delete(entity);
		}
		// 删除角色
		List<Role> roleList = roleService.getAll();
		for (Role entity : roleList) {
			roleService.delete(entity);
		}
		// 删除应用
		List<App> verList = appService.getAll();
		for (App entity : verList) {
			appService.delete(entity);
		}
		// 删除FTP
		List<FtpServer> ftpList = ftpServerService.getAll();
		for (FtpServer entity : ftpList) {
			ftpServerService.delete(entity);
		}
	}

	Set<Role> roleSet = new HashSet<Role>();
	Set<Role> roleSet1 = new HashSet<Role>();
	Set<Role> roleSet2 = new HashSet<Role>();
	Set<Role> roleSet3 = new HashSet<Role>();
	
	private void initRole() {
		logger.debug("初始化角色-开始");
		Role role1 = new Role("一级审核员");
		roleService.save(role1);
		roleSet1.add(role1);
		
		Role role2 = new Role("二级审核员");
		roleService.save(role2);
		roleSet2.add(role2);

		Role role3 = new Role("三级审核员");
		roleService.save(role3);
		roleSet3.add(role3);
		
		Role role = new Role("管理员");
		roleService.save(role);
		logger.debug("初始化角色-完成");
		roleSet.add(role);
	}

	private void initUser() {
		logger.debug("初始化用户-开始");
		
		userService.save(new User("admin", DigestUtils.md5Hex("admin"),
				"admin", roleSet, true));
		userService.save(new User("zhangsan", DigestUtils.md5Hex("123456"),
				"张三", roleSet1, true));
		userService.save(new User("lisi", DigestUtils.md5Hex("123456"), "李四",
				roleSet2, true));
		userService.save(new User("wangwu", DigestUtils.md5Hex("123456"), "王五",
				roleSet3, true));
		
		logger.debug("初始化用户-完成");
	}

	private App app1;
	private App app2;
	private App app3;

	private void initApp() {
		logger.debug("初始化应用应用信息-开始");

		app1 = new App("TQ", "TodayWeather", "天气", false);
		appService.save(app1);
		logger.debug("成功添加应用信息：" + app1);

		app2 = new App("WJ", "PriceOfThings", "物价", false);
		appService.save(app2);
		logger.debug("成功添加应用信息：" + app2);

		app3 = new App("ZS", "LifeIndex", "生活指数", false);
		appService.save(app3);
		logger.debug("成功添加应用信息：" + app3);

		logger.debug("初始化应用应用信息-完成");
	}

	private void initAuditResult() {
/*		auditResultService.save(new AuditResult(app1, role1));
		auditResultService.save(new AuditResult(app1, role2));
		auditResultService.save(new AuditResult(app1, role3));
		auditResultService.save(new AuditResult(app1, role));

		auditResultService.save(new AuditResult(app2, role1));
		auditResultService.save(new AuditResult(app2, role2));
		auditResultService.save(new AuditResult(app2, role3));
		auditResultService.save(new AuditResult(app2, role));

		auditResultService.save(new AuditResult(app3, role1));
		auditResultService.save(new AuditResult(app3, role2));
		auditResultService.save(new AuditResult(app3, role3));
		auditResultService.save(new AuditResult(app3, role));*/

	}

	private void initFTP() {
		logger.debug("初始化FTP-开始");
		ftpServerService
				.save(new FtpServer("127.0.0.1", 21, "mhkw", "mhkw123"));
		logger.debug("初始化FTP-完成");
	}

	private void initIPRange() {
		// 长宁1(定西) CMTS8
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.0.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.74.36.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.2.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.73.248.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.4.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.6.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.8.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.10.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.74.104.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.63.14.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS8", "10.74.92.1",
				"255.255.252.0"));
		// 长宁1(定西) CMTS9
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.63.16.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.63.18.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.73.98.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.63.20.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.63.22.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.63.24.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁1(定西) CMTS9", "10.63.26.1",
				"255.255.254.0"));
		// 长宁3(临空) CMTS10
		ipRangeService.save(new IPRange("长宁3(临空) CMTS10", "10.62.32.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS10", "10.62.34.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS10", "10.62.36.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS10", "10.62.38.1",
				"255.255.254.0"));
		// 长宁3(临空) CMTS9
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.16.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.18.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.20.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.73.82.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.75.80.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.22.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.24.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.26.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.62.30.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.73.164.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS9", "10.75.96.1",
				"255.255.252.0"));
		// 长宁3(临空) CMTS8
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.0.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.74.252.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.2.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.4.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.73.102.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.75.84.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.6.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.73.76.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.75.76.1",
				"255.255.252.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.8.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.10.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.12.1",
				"255.255.254.0"));
		ipRangeService.save(new IPRange("长宁3(临空) CMTS8", "10.62.14.1",
				"255.255.254.0"));
		//
		// NGB
		ipRangeService.save(new IPRange("NGB", "10.163.64.0", "18"));
		ipRangeService.save(new IPRange("NGB", "10.160.0.0", "18"));
		ipRangeService.save(new IPRange("NGB", "10.163.128.0", "17"));
		ipRangeService.save(new IPRange("NGB", "10.161.128.0", "17"));
	}

	@Test
	public void testDBInitData() {
		try {
			clearData();

			// 初始化角色
			initRole();

			// 初始化用户
			initUser();

			// 初始化应用
			initApp();

			// 初始化应用审核
			initAuditResult();

			// 初始化FTP
			initFTP();

			// 初始化区域IP范围
			initIPRange();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
